json

您所在的位置:网站首页 docker hub api json

json

#json| 来源: 网络整理| 查看: 265

json_util --使用Python的工具 json 带有BSON文档的模块¶

使用Python的工具 json 带有BSON文档的模块。

此模块提供两个助手方法 dumps and loads that wrap the native json methods and provide explicit BSON conversion to and from JSON. JSONOptions provides a way to control how JSON is emitted and parsed, with the default being the legacy PyMongo format. json_util can also generate Canonical or Relaxed Extended JSON 什么时候? CANONICAL_JSON_OPTIONS 或 RELAXED_JSON_OPTIONS 分别提供。

示例用法(反序列化):

>>> from bson.json_util import loads >>> loads('[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "80", "$binary": "AQIDBA=="}}]') [{u'foo': [1, 2]}, {u'bar': {u'hello': u'world'}}, {u'code': Code('function x() { return 1; }', {})}, {u'bin': Binary('...', 128)}]

示例用法(序列化):

>>> from bson import Binary, Code >>> from bson.json_util import dumps >>> dumps([{'foo': [1, 2]}, ... {'bar': {'hello': 'world'}}, ... {'code': Code("function x() { return 1; }", {})}, ... {'bin': Binary(b"")}]) '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]'

示例用法(与 CANONICAL_JSON_OPTIONS ):

>>> from bson import Binary, Code >>> from bson.json_util import dumps, CANONICAL_JSON_OPTIONS >>> dumps([{'foo': [1, 2]}, ... {'bar': {'hello': 'world'}}, ... {'code': Code("function x() { return 1; }")}, ... {'bin': Binary(b"")}], ... json_options=CANONICAL_JSON_OPTIONS) '[{"foo": [{"$numberInt": "1"}, {"$numberInt": "2"}]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'

示例用法(与 RELAXED_JSON_OPTIONS ):

>>> from bson import Binary, Code >>> from bson.json_util import dumps, RELAXED_JSON_OPTIONS >>> dumps([{'foo': [1, 2]}, ... {'bar': {'hello': 'world'}}, ... {'code': Code("function x() { return 1; }")}, ... {'bin': Binary(b"")}], ... json_options=RELAXED_JSON_OPTIONS) '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'

或者,您可以手动传递 default 到 json.dumps() . 它处理不了 Binary 和 Code 实例(因为它们是扩展字符串,所以不能提供自定义默认值),但由于递归较少,所以速度会更快。

注解

如果您的应用程序不需要 JSONOptions 花了大量的时间在 json_util module, look to python-bsonjs 为了提高性能。 python-bsonjs is a fast BSON to MongoDB Extended JSON converter for Python built on top of libbson . python-bsonjs 使用时与PyMongo配合使用效果最佳 RawBSONDocument .

在 2.8 版更改: The output format for Timestamp has changed from '{"t": , "i": }' to '{"$timestamp": {"t": , "i": }}'. This new format will be decoded to an instance of Timestamp. The old format will continue to be decoded to a python dict as before. Encoding to the old format is no longer supported as it was never correct and loses type information. Added support for $numberLong and $undefined - new in MongoDB 2.6 - and parsing $date in ISO-8601 format.

在 2.7 版更改: 在呈现SON、Timestamp、Code、Binary和DBRef实例时保持顺序。

在 2.3 版更改: 添加了dumps和loads助手来自动处理与json的转换,并支持 Binary 和 Code

class bson.json_util.DatetimeRepresentation¶ LEGACY = 0¶

遗留的MongoDB扩展JSON日期时间表示。

datetime.datetime 实例将以格式编码为JSON {{"$date": }} 在哪里 dateAsMilliseconds 是一个64位有符号整数,给出自Unix纪元UTC以来的毫秒数。这是PyMongo 3.4版本之前的默认编码。

3.4 新版功能.

NUMBERLONG = 1¶

数字长日期时间表示。

datetime.datetime 实例将以格式编码为JSON {{"$date": {{"$numberLong": ""}}}} 在哪里 dateAsMilliseconds 是64位带符号整数的字符串表示形式,该整数表示自Unix纪元UTC以来的毫秒数。

3.4 新版功能.

ISO8601 = 2¶

ISO-8601日期时间表示法。

datetime.datetime 大于或等于Unix epoch UTC的实例将以格式编码为JSON {{"$date": ""}} . datetime.datetime Unix epoch UTC之前的实例将被编码,就像日期时间表示是 NUMBERLONG .

3.4 新版功能.

class bson.json_util.JSONMode¶ LEGACY = 0¶

旧的扩展JSON表示。

在这种模式下, dumps() 生成PyMongo的遗留非标准JSON输出。考虑使用 RELAXED 或 CANONICAL 相反。

3.5 新版功能.

RELAXED = 1¶

宽松的扩展JSON表示。

在这种模式下, dumps() 生成宽松的扩展JSON,一种基本上类似于JSON的格式。考虑将其用于webapi,其中发送的文档(或文档的投影)只使用普通JSON类型原语。尤其是 int , Int64 和 float 数字类型以本机JSON数字格式表示。这个输出也是最可读的,对调试和文档非常有用。

参见

放松的规范 Extended JSON .

3.5 新版功能.

CANONICAL = 2¶

规范的扩展JSON表示。

在这种模式下, dumps() 生成规范的扩展JSON,一种类型保留格式。考虑将其用于测试之类的事情,在测试中必须精确地指定JSON中的预期类型。尤其是 int , Int64 和 float 数值类型用类型包装器编码。

参见

规范规范 Extended JSON .

3.5 新版功能.

class bson.json_util.JSONOptions(strict_number_long=False, datetime_representation=0, strict_uuid=False, json_mode=0, *args, **kwargs)¶

创建CodecOptions的新实例(document_class,tz_aware,uuid_表示,unicode_decode_error_handler,tzinfo,type_registry)

bson.json_util.LEGACY_JSON_OPTIONS = JSONOptions(strict_number_long=False, datetime_representation=0, strict_uuid=False, json_mode=0, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None))¶

JSONOptions 将JSON转换为Pyo的旧格式。

参见

文件 bson.json_util.JSONMode.LEGACY .

3.5 新版功能.

bson.json_util.DEFAULT_JSON_OPTIONS = JSONOptions(strict_number_long=False, datetime_representation=0, strict_uuid=False, json_mode=0, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None))¶

默认值 JSONOptions 用于JSON编码/解码。

一样 LEGACY_JSON_OPTIONS . 这将更改为 RELAXED_JSON_OPTIONS 在未来的版本中。

3.4 新版功能.

bson.json_util.CANONICAL_JSON_OPTIONS = JSONOptions(strict_number_long=True, datetime_representation=1, strict_uuid=True, json_mode=2, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None))¶

JSONOptions 对于规范的扩展JSON。

参见

文件 bson.json_util.JSONMode.CANONICAL .

3.5 新版功能.

bson.json_util.RELAXED_JSON_OPTIONS = JSONOptions(strict_number_long=False, datetime_representation=2, strict_uuid=True, json_mode=1, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None))¶

JSONOptions 对于宽松的扩展JSON。

参见

文件 bson.json_util.JSONMode.RELAXED .

3.5 新版功能.

bson.json_util.STRICT_JSON_OPTIONS = JSONOptions(strict_number_long=True, datetime_representation=2, strict_uuid=True, json_mode=0, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None))¶

DEPRECATED - JSONOptions 对于MongoDB扩展JSON的 严格模式 编码。

3.4 新版功能.

在 3.5 版更改: 不赞成的使用 RELAXED_JSON_OPTIONS 或 CANONICAL_JSON_OPTIONS 相反。

bson.json_util.dumps(obj, *args, **kwargs)¶

包装的助手函数 json.dumps() .

处理所有BSON类型的递归函数,包括 Binary 和 Code .

参数

json_options 答: JSONOptions 实例,用于修改MongoDB扩展JSON类型的编码。默认为 DEFAULT_JSON_OPTIONS .

在 3.4 版更改: 接受可选参数 json_options . 见 JSONOptions .

在 2.7 版更改: 在呈现SON、Timestamp、Code、Binary和DBRef实例时保持顺序。

bson.json_util.loads(s, *args, **kwargs)¶

包装的助手函数 json.loads() .

自动传递用于BSON类型转换的object_钩子。

加薪 TypeError , ValueError , KeyError 或 InvalidId 在无效的MongoDB扩展JSON上。

参数

json_options 答: JSONOptions 实例,用于修改MongoDB扩展JSON类型的解码。默认为 DEFAULT_JSON_OPTIONS .

在 3.5 版更改: 解析宽松和规范的扩展JSON以及PyMongo的遗留格式。现在提高 TypeError 或 ValueError 使用错误类型的值或任何额外的键解析JSON类型包装器时。

在 3.4 版更改: 接受可选参数 json_options . 见 JSONOptions .

bson.json_util.object_pairs_hook(pairs, json_options=JSONOptions(strict_number_long=False, datetime_representation=0, strict_uuid=False, json_mode=0, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))¶ bson.json_util.object_hook(dct, json_options=JSONOptions(strict_number_long=False, datetime_representation=0, strict_uuid=False, json_mode=0, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))¶ bson.json_util.default(obj, json_options=JSONOptions(strict_number_long=False, datetime_representation=0, strict_uuid=False, json_mode=0, document_class=dict, tz_aware=True, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))¶


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3